package com.lottery.ruok.lottrey.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.ext.GlideExt;
import com.lottery.ruok.lottrey.retrofitModule.response.ActivityListResponse;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.DisplayUtil;

import butterknife.BindView;
import butterknife.OnClick;
import utils.Utils;

/**
 * 首页的活动卡片
 */
public class ActivitiesView extends BaseRelativeLayout {
    @BindView(R.id.tv_activities_title)
    TextView tvActivitiesTitle;
    @BindView(R.id.tv_term_of_validity)
    TextView tvTermOfValidity;
    @BindView(R.id.ll_activities_titlebar)
    LinearLayout llActivitiesTitlebar;
    @BindView(R.id.activities_container)
    HorizontalScrollView activitiesContainer;
    @BindView(R.id.ll_container)
    LinearLayout llContainer;
    @BindView(R.id.image_ultimate)
    ImageView imageUltimate;
    @BindView(R.id.ll_root)
    RelativeLayout llRoot;
    @BindView(R.id.missions_title)
    TextView missionsTitle;
    @BindView(R.id.rl_top_bg)
    RelativeLayout rlTopBg;
    private ActivityListResponse.ListBean mData;
    private OnActivitiesCardClickListener mOnActivitiesCardClickListener;

    public ActivitiesView(Context context) {
        super(context);
    }

    public ActivitiesView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ActivitiesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.activities_layout);
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(ActivityListResponse.ListBean mData) {
        this.mData = mData;
        updateView();
    }

    private void updateView() {
        if (mData != null) {
            ActivityListResponse.ListBean data = mData;
            //设置title内容
            tvActivitiesTitle.setText(data.getName());
            //title字体颜色
            tvActivitiesTitle.setTextColor(Color.parseColor(data.getStyles().getTitle_color()));
            //有效期
            if (data.getShort_time()!=null) {
                tvTermOfValidity.setText("活动期间：" + data.getShort_time());
            }

            //设置整个背景色
            if (mData.getStyles().getBackground_theme()!=null) {
                llRoot.setBackgroundColor(Color.parseColor(mData.getStyles().getBackground_theme()));
            }


            //设置头部背景
            GlideExt.setBackgroundDrawable(mContext, data.getStyles().getHeader(), llActivitiesTitlebar, R.mipmap.activities_top);

            if (mData.getType() == 3) {//是否是c类任务
                GlideExt.setBackgroundDrawable(mContext, data.getStyles().getAward(), imageUltimate, R.mipmap.award);
            } else {
                imageUltimate.setVisibility(GONE);
            }

            if ((mData).getMissions().size() == 1) {//如果只有一个任务
                ActivitiesSingleMissions activitiesSingleMissions = new ActivitiesSingleMissions(mContext);
                activitiesSingleMissions.setData(mData.getMissions().get(0), (mData).getId() + "", mData.getStatus());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.topMargin = 30;
                activitiesSingleMissions.setLayoutParams(layoutParams);
                llContainer.addView(activitiesSingleMissions);
                missionsTitle.setVisibility(GONE);
            } else {//有多个任务
                int firstProcessing = 1;
                for (int i = 0; i < mData.getMissions().size(); i++) {
                    ActivityListResponse.ListBean.MissionsBean missionsBean = mData.getMissions().get(i);
                    if (missionsBean.getStatus() == 1 && firstProcessing == 1) {//这段代码主要是为了只让任务有发光效果，阶段奖励和终极大奖不发光
                        firstProcessing = -1;
                    } else if (missionsBean.getStatus() == 1 && firstProcessing == -1) {
                        missionsBean.setStatus(0);
                    }
                    ActivitiesItemView activitiesItemView = new ActivitiesItemView(mContext);
                    activitiesItemView.setData(mData.getMissions(),missionsBean, (mData).getId() + "", mData.getStatus(), i);
                    llContainer.addView(activitiesItemView);

                    if (missionsBean.getStatus() == 1) {
                        missionsTitle.setVisibility(VISIBLE);
                        missionsTitle.setText(missionsBean.getTitle().getValue());
                        missionsTitle.setTextColor(Color.parseColor(missionsBean.getTitle().getColor()));
                    } else {
                        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) activitiesItemView.getLayoutParams();
                        lp.topMargin = (int) DisplayUtil.dip2px(5f, mContext);
                        activitiesItemView.setLayoutParams(lp);
                    }
                    if (i == 0 && Utils.isEmpty(UserInfo.getToken())) {
                        missionsTitle.setVisibility(VISIBLE);
                        missionsTitle.setText(missionsBean.getTitle().getValue());
                        missionsTitle.setTextColor(Color.parseColor(missionsBean.getTitle().getColor()));
                    }

                }
            }
        } else {
            /**
             * 在未参与或加入活动的初始化默认的数据
             */
            ActivitiesItemView activitiesItemView = new ActivitiesItemView(mContext);
            llContainer.addView(activitiesItemView);

            ActivitiesItemView activitiesItemView0 = new ActivitiesItemView(mContext);
            activitiesItemView0.setProgressBarVisible();
            activitiesItemView0.setActivitiesImage(R.mipmap.activitymoney);
            activitiesItemView0.setActivitiesContent("奖金\n5,0000元...");
            llContainer.addView(activitiesItemView0);

            ActivitiesItemView activitiesItemView1 = new ActivitiesItemView(mContext);
            activitiesItemView1.setProgressBarVisible();
            activitiesItemView1.setActivitiesImage(R.mipmap.activityredbag);
            activitiesItemView1.setActivitiesContent("抽红包");
            llContainer.addView(activitiesItemView1);


            ActivitiesItemView activitiesItemView2 = new ActivitiesItemView(mContext);
            activitiesItemView2.setProgressBarVisible();
            activitiesItemView2.setActivitiesImage(R.mipmap.activityothers);
            activitiesItemView2.setActivitiesContent("豪华礼包");
            llContainer.addView(activitiesItemView2);
            imageUltimate.setVisibility(GONE);
        }
     /*   if (llContainer.getChildCount() <= 4) {
            HorizontalScrollView.LayoutParams layoutParams = new HorizontalScrollView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
            llContainer.setLayoutParams(layoutParams);
        }*/
    }


    @OnClick({R.id.image_ultimate, R.id.rl_top_bg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_ultimate:
                activitiesContainer.fullScroll(ScrollView.FOCUS_RIGHT);
                break;
            case R.id.rl_top_bg:
                if (mOnActivitiesCardClickListener != null) {
                    mOnActivitiesCardClickListener.onClick();
                }
                break;
        }
    }

    public void setOnActivitiesCardClickListener(OnActivitiesCardClickListener listener) {
        this.mOnActivitiesCardClickListener = listener;
    }

    public interface OnActivitiesCardClickListener {
        void onClick();
    }
}


